/* 产品 */

var ElementBase = require("./ElementBase.js");

function ElementProduct() {
	//继承基类
	ElementBase.apply(this, arguments);

	this.type = "product";
	this.textureUrl = "";
	
	this.crop = "";
	
	this.bgPath = null;
	this.cropPath = null;
	this._w = 1;
	this._h = 1;
	
	this.designBounds = null; //可编辑区域
	this.printWidth = null; //真实可编辑区域尺寸（单位cm）
	this.printHeight = null;
	
	this.designType = 0;//0可编辑区域设计，1全衣设计
	this.realScale = 2;
}

ElementProduct.prototype = new ElementBase();

/*
 * 设置图片信息
 */
ElementProduct.prototype.setJson = function($data, calBack) {
	var self = this;
	this.super_setJson($data);
	
	var product = Design.mainEditer.product;
	
	if($data.designType != undefined){
		this.designType = $data.designType;
	}
	
	var designBounds = $data.designBounds;
	if(designBounds){
		self.designBounds = designBounds;
		var x = designBounds.x;
		var y = designBounds.y;
		var w = designBounds.w;
		var h = designBounds.h;
		self.cropPath = Design.paper.rect(x, y, w, h);
		
		if($data.printWidth != undefined){
			self.printWidth = $data.printWidth;
			self.realScale = self.printWidth * 28.346 / w; //72dpi 1厘米=28.346像素
		}
		self.cropPath.appendTo(self.face.productFg);
		self.cropPath.attr({
			"fill": "none",
			"stroke": "#08F",
			"stroke-width": 2,
			"stroke-dasharray": "6 3",
			"displayType": "cropPath"
		})
	}
	
	var textureUrl = $data.texture;
	
	if(textureUrl) {
		self.textureUrl = textureUrl;
		//前景
		self.loadImg(textureUrl, function(img,base64) {
			if(self.isdestroy) return;
			
			//背景
			self.bgPath = Design.paper.rect(0, 0, img.width, img.height);
			var fillcolor = "none";
			if(product.currColor){
				fillcolor = product.currColor.color;
			}
			self.bgPath.attr({
				"displayType":"bgPath",
				"fill":fillcolor
			});
			self.bgPath.appendTo(self.face.productBg);
			
			self.bgPath.mousedown(function(e) {
				self.bgPath.mousedownEvent = e;
				self.select.call(self);
			});
			
			//裁剪路径
			if(self.cropPath){
				self.cropPath.appendTo(self.face.productFg);
				self.cropPath.attr({
					"fill": "none",
					"stroke": "#08F",
					"stroke-width": 2,
					"stroke-dasharray": "6 3",
					"displayType": "cropPath"
				})
				
				//全衣设计，隐藏线框
				if(self.designType == 1){
					self.cropPath.attr({
						"stroke": "none"
					})
				}
			}
			
			self.element = Design.paper.image(base64);
			self.element.attr("pointer-events", "none");
			self.appendToCon();
			self._w = img.width;
			self._h = img.height;
			var tx = (Design.stageW - self._w)/2;
			var ty = (Design.stageH - self._h)/2;
			Design.mainEditer.moveTo(tx, ty);
			self.createEnd();
			if(calBack){
				calBack();
			}
		});
	}
}

ElementProduct.prototype.updateProLayer = function(){
	this.productlayer(this.designType);
}

/*
 * 获取保存的数据
 */
ElementProduct.prototype.getSaveData = function() {
	var json = this.super_getSaveData();
	json.url = this.url;
	json.repeat = this.repeat;
	json.marginx = this.marginx;
	json.marginy = this.marginy;
	json.crop = this.crop;
	return json;
}

ElementProduct.prototype.getEditeRect = function(){
	if(this.cropPath){
		return this.cropPath.getBBox();
	}else{
		return this.element.getBBox();
	}
}

ElementProduct.prototype.setColor = function(obj){
	if(this.bgPath){
		this.bgPath.attr("fill", obj.image);
	}
}

ElementProduct.prototype.saveUI = function(){
	if(this.cropPath){
		this.cropPath.attr({
			"stroke": "none",
		})
	}
	
	if(this.element) {
		this.element.attr("opacity", 0.01);
	}
}

/*
 * 隐藏
 */
ElementProduct.prototype.hide = function(){
	if(this.element) {
		this.element.attr("display", "none");
	}
	if(this.rectPath){
		this.rectPath.attr("display", "none");
	}
}

/*
 * 显示
 */
ElementProduct.prototype.show = function(){
	if(this.element) {
		this.element.attr("display", "");
	}
	if(this.rectPath){
		this.rectPath.attr("display", "");
	}
}

//销毁
ElementProduct.prototype.destroy = function() {
	this.isdestroy = true;
	var currele = Design.tool.getTarget();
	if(this == currele){
		Design.tool.setTarget(null);
	}
	this.unselect();
	if(this.element) {
		this.element.remove();
		this.element = null;
	}
	if(this.rectPath) {
		this.rectPath.remove();
		this.rectPath = null;
	}
}

/*
 * 选中
 */
ElementProduct.prototype.select = function() {
	Design.tool.setTarget(null);
	Design.view.bind(this);
	Design.view.layer.select(this);
}

/*
 * 取消选中
 */
ElementProduct.prototype.unselect = function() {
	Design.view.unbind(this);
	Design.view.layer.unselect(this);
}


ElementProduct.prototype.getIcon = function(){
	return '<img alt="" src="'+this.url+'"><span>背景</span>';
}

exports = module.exports = ElementProduct;